package com.ruoyi.web.controller.common;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.redis.QueueUtils;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.system.domain.bo.StockListBo;
import com.ruoyi.system.domain.vo.StockListVo;
import com.ruoyi.system.domain.vo.VariousKindsPicVo;
import com.ruoyi.system.domain.vo.VariousKindsVideoVo;
import com.ruoyi.system.service.IStockListService;
import com.ruoyi.system.service.IVariousKindsPicService;
import com.ruoyi.system.service.IVariousKindsVideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.ruoyi.web.controller.common.ApiUtil.sendWenDaMsg;
import static com.ruoyi.web.controller.common.MatchUtil.*;
import static com.ruoyi.web.controller.common.RobotConstant.*;
import static com.ruoyi.web.controller.common.RobotFeatureEnum.*;

/**
 * 验证码操作处理
 *
 * @author Lion Li
 */
@RestController
@RequiredArgsConstructor
public class TestRobotController {
    private final IVariousKindsPicService picService;
    private final IVariousKindsVideoService videoService;


    static {
        QueueUtils.subscribeBlockingQueue(DELAYED_QUEUE_WENDA, (TianEntity entity) -> {
            sendWenDaMsg(entity);
        });
    }

    /**
     * 生成验证码
     */
    @PostMapping("/robot")
    public String getCode(@RequestBody WxRecMessage message) {
        System.out.println("message = " + message);
        String s1 = "";
        Map map = new HashMap(8);
        map.put("success", Boolean.TRUE);
        map.put("robot_wxid", message.getRobot_wxid());
        map.put("to_wxid", message.getFrom_wxid());
        map.put("group_wxid", message.getFrom_wxid());
        map.put("member_wxid", message.getFinal_from_wxid());
        map.put("member_name", message.getFinal_from_name());
        TianEntity cacheObject = RedisUtils.getCacheObject(ROBOT_PREFIX + message.getFrom_wxid());
        // 该房间有任务，则不需要艾特则回答
        if (ObjectUtil.isNotEmpty(cacheObject)) {
            map.put("event", "SendGroupMsgAndAt");
            if (message.getMsg().contains(cacheObject.getResult())) {
                // 回答成功则删除redis
                map.put("msg", getCorrectResponse(cacheObject));
                QueueUtils.removeDelayedQueueObject(DELAYED_QUEUE_WENDA, cacheObject);
                RedisUtils.deleteObject(ROBOT_PREFIX + message.getFrom_wxid());
            } else if (StrUtil.equals("过", getInputMsg(message.getMsg()))) {
                map.put("msg", getCorrectResponse(cacheObject, "robot"));
                QueueUtils.removeDelayedQueueObject(DELAYED_QUEUE_WENDA, cacheObject);
                RedisUtils.deleteObject(ROBOT_PREFIX + message.getFrom_wxid());
            } else {
                // 回答失败不进行操作
                map.put("msg", "不对哦，还剩下" + RedisUtils.getTimeToLive(ROBOT_PREFIX + message.getFrom_wxid()) / 1000 + "秒钟,加油!");
            }
        } else if (message.getMsg().contains(AT_EG) || message.getMsg().contains(AT_EG2) || message.getMsg().contains(AT_EG3)) {
            if (message.getMsg().contains("菜单") || message.getMsg().contains("功能")) {
                String simpleTalk = ApiUtil.getRobotMenu();
                map.put("event", "SendTextMsg");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(TIAN_GOU.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(TIAN_GOU);
                map.put("event", "SendTextMsg");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(SAY_LOVE.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(SAY_LOVE);
                map.put("event", "SendTextMsg");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(STORY.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(STORY);
                map.put("event", "SendTextMsg");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(JOKER.codeName())) {
                String simpleTalk = ApiUtil.getFreeSimpleTalk(JOKER);
                map.put("event", "SendTextMsg");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(SEXY_PIC.codeName())) {
                map.put("event", "SendImageMsg");
                Map map1 = new HashMap(8);
                VariousKindsPicVo vo = picService.selectRandomOne();
                if (ObjectUtil.isNull(vo)) {
                    map1.put("url", "https://b3logfile.com/bing/20201024.jpg");
                    map1.put("name", "20201024.jpg");
                } else {
                    map1.put("url", vo.getPicAddress());
                    map1.put("name", vo.getPicId() + MatchUtil.getMatch(vo.getPicAddress()));
                }
                map.put("msg", map1);
            } else if (message.getMsg().contains(WOXIANGTING.codeName())) {
                String inputMsg = MatchUtil.getInputMsg(message.getMsg(), WOXIANGTING);
                map.put("event", "SendMusicMsg");
                Map map1 = new HashMap(8);
                map1.put("name", inputMsg);
                map1.put("type", "0");
                map.put("msg", map1);
            } else if (message.getMsg().contains(MUSIC.codeName())) {
                String inputMsg = MatchUtil.getInputMsg(message.getMsg(), MUSIC);
                map.put("event", "SendMusicMsg");
                Map map1 = new HashMap(8);
                if (StrUtil.isBlank(inputMsg)) {
                    inputMsg = DEFAULT_MUSCI.get(RandomUtil.randomInt(0, DEFAULT_MUSCI.size()));
                }
                map1.put("name", inputMsg);
                map1.put("type", "0");
                map.put("msg", map1);
            } else if (message.getMsg().contains(DOUYIN_VIDEO.codeName())) {
                map.put("event", "SendVideoMsg");
                Map map1 = new HashMap(8);
//                VariousKindsVideoVo vo = videoService.selectRandomVideo();
                int i = RandomUtil.randomInt(1, 320);
                map1.put("url", "http://localhost:8080/video/" + i + ".mp4");
                map1.put("name", i + ".mp4");
                map.put("msg", map1);
            } else if (message.getMsg().contains(CAIHONGPI.codeName()) || message.getMsg().contains(KUAWO.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(CAIHONGPI);
                map.put("event", "SendGroupMsgAndAt");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(ZAOAN.codeName()) || message.getMsg().contains(MORNING.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(ZAOAN);
                map.put("event", "SendGroupMsgAndAt");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(WANAN.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(WANAN);
                map.put("event", "SendGroupMsgAndAt");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(DUJITANG.codeName())) {
                String simpleTalk = ApiUtil.getSimpleTalk(DUJITANG);
                map.put("event", "SendGroupMsgAndAt");
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(CANGTOUSHI.codeName())) {
                String inputMsg = MatchUtil.getInputMsg(message.getMsg(), CANGTOUSHI);
                map.put("event", "SendGroupMsgAndAt");
                if (inputMsg.length() < 2 || inputMsg.length() > 8) {
                    map.put("msg", CANGTOUSHI_HINT);
                } else {
                    String simpleTalk = ApiUtil.getCangShiTalk(CANGTOUSHI, inputMsg);
                    map.put("msg", simpleTalk);
                }
            } else if (message.getMsg().contains(LUNAR.codeName())) {
                String inputMsg = getHuangLiDay(message.getMsg());
                map.put("event", "SendGroupMsgAndAt");
                if (inputMsg.length() != DatePattern.NORM_DATE_PATTERN.length()) {
                    map.put("msg", LUNAR_HINT);
                } else {
                    String simpleTalk = ApiUtil.getHuangLiTalk(LUNAR, inputMsg);
                    map.put("msg", simpleTalk);
                }
            } else if (message.getMsg().contains(STOCK_ZHI_SHU.codeName())) {
                map.put("event", "SendGroupMsgAndAt");
                String simpleTalk = ApiUtil.getZhiShuInfo();
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(STOCK.codeName())) {
                String inputMsg = getInputMsg(message.getMsg(), STOCK);
                map.put("event", "SendGroupMsgAndAt");
                String simpleTalk = ApiUtil.getStockPriceInfo(inputMsg);
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(WEN_DA.codeName())) {
                map.put("event", "SendTextMsg");
                String simpleTalk = ApiUtil.getComplexTalk(WEN_DA, message);
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(SLOGAN.codeName())) {
                map.put("event", "SendTextMsg");
                String simpleTalk = ApiUtil.getComplexTalk(SLOGAN, message);
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(PROVERB.codeName())) {
                map.put("event", "SendTextMsg");
                String simpleTalk = ApiUtil.getComplexTalk(PROVERB, message);
                map.put("msg", simpleTalk);
            } else if (message.getMsg().contains(ZIMI.codeName())) {
                map.put("event", "SendTextMsg");
                String simpleTalk = ApiUtil.getComplexTalk(ZIMI, message);
                map.put("msg", simpleTalk);
            } else {
                map.put("event", "SendGroupMsgAndAt");
                String simpleTalk = ApiUtil.getOwnThinkTalk(message);
                map.put("msg", simpleTalk);
            }
        }
        s1 = JSON.toJSONString(map, SerializerFeature.BrowserCompatible);
        return s1;
    }

    /**
     * test
     */
    @GetMapping("/robot/test")
    public R<String> test() {
        Map<String, Object> map = new HashMap<>(8);
        map.put("event", "SendVideoMsg");
        map.put("robot_wxid", "wxid_shhrqqr93hib22");
        map.put("to_wxid", "23712025745@chatroom");
        Map map1 = new HashMap(8);
        map1.put("name", "aaa.mp4");
        map1.put("url", "https://v.douyin.com/NUTQhvA/");
        String s1 = JSON.toJSONString(map, SerializerFeature.BrowserCompatible);
        String body = HttpRequest.post(SEND_ROBOT_URL)
            .header("Content-type", "application/json;charset=utf-8")
            .body(s1)
            .timeout(20000)
            .execute().body();
        return R.ok(body);
    }

    public static void main(String[] args) {
//        String path = "https://b3logfile.com/bing/20201024.jpg";
//        String suffixes="jpeg|jpg|png";
//        Pattern pat=Pattern.compile("[\\w]+[\\.]("+suffixes+")");//正则判断
//        Matcher mc=pat.matcher(path);//条件匹配
//        while(mc.find()) {
//            String fileName= mc.group();//截取文件名后缀名
//            System.out.println("fileName = " + fileName);
//        }
//        String simpleTalk = ApiUtil.getHuangLiTalk(CANGTOUSHI,"2022-11-22");
//        System.out.println("simpleTalk = " + simpleTalk);
    }
}
